<?php

namespace App\Http\Controllers;

use App\Http\Requests\IndexCommissionRequest;
use App\Http\Requests\UpdateCommissionRequest;
use App\Http\Resources\CommissionResource;
use App\Models\Admin;
use App\Models\Commission;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class CommissionController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:sanctum');
    }
    /**
     * Display a listing of the resource.
     */
    public function index(IndexCommissionRequest $request)
    {
        $commission = Commission::query();

        $commission->when($request->filled('username'), function ($commission) use ($request) {
            $username = $request->input('username');
            $commission->whereHas('admins', function ($query) use ($username) {
                $query->where('username', 'like', "%$username%");
            });
        });

        $data = $commission->with(['admins.admins'])->paginate(
            $request->query('pageSize'),
            ['*'],
            'page',
            $request->query('pageNum')
        );
        $response = CommissionResource::collection($data);
        $total = $response->total(); // 获取分页信息中的总数
        return $this->success('查询成功', $response, $total);
    }

    public function show(Request $request, Commission $commission)
    {
        if (!isSuperadmin()) return $this->error('暂无权限');
        $data = $commission->load('admins');
        $response = new CommissionResource($data);
        return $this->success('查询成功', $response);
    }


    public function update(UpdateCommissionRequest $request, Commission $commission)
    {
        $commission->fill($request->input());
        $commission->save();

        return $this->success('更新佣金配置成功', $commission);
    }

    public function defaultOption(Request $request)
    {
        if (!isSuperadmin()) {
            return $this->error('暂无权限');
        }
        // 当前文件的目录
        $currentDirectory = __DIR__;
        // 获取目录分隔符
        $separator = DIRECTORY_SEPARATOR;
        for ($i = 0; $i < 3; $i++) {
            $currentDirectory = dirname($currentDirectory);
        }
        $file_name = $currentDirectory . $separator . 'config' . $separator . 'defaultconfig.php';
        $data = include  $file_name;
        return $this->success('查询成功', $data);
    }

    public function updateDefaultOption(Request $request)
    {
        if (!isSuperadmin()) {
            return $this->error('暂无权限');
        }
        Validator::make($request->input(), [
            "commission_top_rate" => ['required'],
            "commission_sup_rate" => ['required'],
        ])->validate();
        // 当前文件的目录
        $currentDirectory = __DIR__;
        // 获取目录分隔符
        $separator = DIRECTORY_SEPARATOR;
        for ($i = 0; $i < 3; $i++) {
            $currentDirectory = dirname($currentDirectory);
        }
        $file_name = $currentDirectory . $separator . 'config' . $separator . 'defaultconfig.php';
        $data = $request->input();
        // 将数组转为字符串，以便存入文件
        $dataAsString = var_export($data, true);
        // 将数据写入文件
        file_put_contents($file_name, "<?php\nreturn " . $dataAsString . ";\n");
        // file_put_contents($file_name);
        return $this->success('跟新默认成功');
    }
}
